package com.koron.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.device.bean.po.EquipmentDocument;
import com.koron.device.bean.query.EquipmentDocumentQuery;
import com.koron.device.mapper.EquipmentDocumentMapper;
import com.koron.device.service.EquipmentDocumentService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class EquipmentDocumentServiceImpl implements EquipmentDocumentService {


    @TaskAnnotation("saveEquipmentDocument")
    @Override
    public Response saveEquipmentDocument(SessionFactory factory, List<EquipmentDocument> equipmentDocuments) {

        log.info("invoke saveEquipmentDocument, equipmentDocuments.size ={}", equipmentDocuments.size());

        if (CollectionUtil.isEmpty(equipmentDocuments)) {
            return Response.fail();
        }

        EquipmentDocumentMapper mapper = factory.getMapper(EquipmentDocumentMapper.class);
        boolean result = mapper.saveEquipmentDocument(equipmentDocuments);
        log.info("invoke saveEquipmentDocument, result = {}", result);
        return Response.ok(result);
    }

    @TaskAnnotation("deleteEquipmentDocument")
    @Override
    public Response deleteEquipmentDocument(SessionFactory factory, List<String> ids) {
        log.info("invoke saveEquipmentDocument, equipmentDocuments.size ={}", ids.size());
        if (CollectionUtil.isEmpty(ids)) {
            return Response.fail();
        }
        EquipmentDocumentMapper mapper = factory.getMapper(EquipmentDocumentMapper.class);
        boolean result = mapper.deleteEquipmentDocument(ids);
        log.info("invoke deleteEquipmentDocument, result = {}", result);
        return Response.ok(result);
    }

    @TaskAnnotation("queryEquipmentDocumentList")
    @Override
    public Response queryEquipmentDocumentList(SessionFactory factory, PageQuery pageQuery, EquipmentDocumentQuery equipmentDocumentQuery) {
        log.info("invoke queryEquipmentDocumentList, equipmentDocumentQuery = {}", JSON.toJSONString(equipmentDocumentQuery));
        EquipmentDocumentMapper mapper = factory.getMapper(EquipmentDocumentMapper.class);
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        // 如果不需要归集查询
        if (equipmentDocumentQuery.isSearch()){
            String fullPath = equipmentDocumentQuery.getFullPath();
            Assert.notNull(fullPath, "设备全路径不能为空！");
            String currPath = fullPath.substring(fullPath.lastIndexOf("/")+1);
            equipmentDocumentQuery.setCurrPath(currPath);
            equipmentDocumentQuery.setFullPath(null);
        }

        List<EquipmentDocument> documentList = mapper.queryEquipmentDocumentList(equipmentDocumentQuery);
        log.info("invoke queryEquipmentDocumentList, documentList.size = {}", documentList.size());
        return Response.ok(new PageInfo<>(documentList));
    }
}
